<?php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/9/15
 * Time: 下午6:42
 */
namespace app\common\model;

use think\facade\Request;
use think\Model;
use think\facade\Cache;

class StoreModel extends Model
{
    //状态
    const STATUS_SHOW=1; //显示
    const STATUS_HIDE=0; //禁用
    const STATUS_DEL=-1; //删除

    //入驻类型
    const TYPE_R=0; //入驻商家
    const TYPE_Z=1; //自营商品

    //关店状态
    const STORE_OPEN=0; //店铺营业中
    const STORE_CLOSE=1; //店铺已关

    //证件类型
    const TYPE_PERSON=1; //个人
    const TYPE_COMPANY=2; //企业

    //入驻身份信息认证状态
    const APPLY_STATUS_STAND=0; //待审核
    const APPLY_STATUS_WAIT=1; //审核中
    const APPLY_STATUS_YES=2; //审核通过
    const APPLY_STATUS_NO=3; //拒绝通过

    protected $autoWriteTimestamp = true;


    /**
     * 所有分组
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getALL($where=[],$type=self::TYPE_Z,$pageSum=0)
    {
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):10;
        }

        $list=self::where($where)
            ->where('type',$type)
            ->order(['list_order'=>'asc','create_time'=>'asc'])
            ->paginate($pageSum);

        return $list;
    }

    /**
     * 增加/编辑
     * @param $data
     * @return array
     * @throws \think\Exception\DbException
     */
    public function adds($data){
        //检测当前应用方法名是否重复
        if(!self::chackAction($data)){
            return['status'=>0,'msg'=>'用户已存在！'];
        }

        if(empty($data['id'])){
            $data["mobile"]=$data["username"];
            $salt=random_string();;
            $data["salt"]=$salt;
            $data["nickname"]=$data["real_name"];
            $data["password"]=password($data["password"],$salt);

            $status = self::save($data);

            if($status){
                return['status'=>1,'msg'=>'增加成功！'];
            }else{
                return['status'=>0,'msg'=>'增加失败！'];
            }
        }else{
            $info=self::field("salt")->get($data["id"]);

            $data["password"]=password($data["password"],$info["salt"]);

            $status = self::save($data,['id'=>$data["id"]]);

            if($status){
                return['status'=>1,'msg'=>'修改成功！'];
            }else{
                return['status'=>0,'msg'=>'修改失败！'];
            }
        }

    }

    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id){

        if($id==self::SUPER_USER_ID){
            return ['status'=>0,'msg'=>'不能删除超级管理员！'];
        }

        $au=self::get($id);
        $au->status=self::STATUS_DEL;
        $au->delete_time=time();

        $status=$au->save();
        if($status){
            return ['status'=>1,'msg'=>'删除成功！'];
        }else{
            return ['status'=>0,'msg'=>'删除失败！'];
        }

    }

    /**
     * 检测当前是否重复
     * @param $data
     * @return bool
     */
    private static function chackAction($data){
        if(empty($data['id'])){
            $data['id']=0;
        }

        $count=self::where('username',$data['username'])
            ->where('id',"<>",$data['id'])
            ->where('id',"<>",self::STATUS_DEL)
            ->count();

        if($count>0){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 检测后台登陆
     * @param $username
     * @param $pwd
     * @return array|null|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function check_login($username,$pwd){
        $info=self::where('username',$username)->find();

        if(empty($info)){
            return ['status'=>0,'msg'=>'管理员不存在！'];
        }

        if($info['status']!=self::STATUS_SHOW){
            return ['status'=>0,'msg'=>'管理员已禁用！'];
        }

        if(password($pwd,$info['salt'])!=$info['password']){
            return ['status'=>0,'msg'=>'密码错误！'];
        }

        return ['status'=>1,'data'=>$info];
    }

}